/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.limousine.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import limousine.vo.CarRent;
import limousine.vo.Driver;

/**
 *
 * @author krisada
 */
public class CarRentService implements Service {

    public DataSource dataSource = null;
    private int limit = 20;

    public CarRentService() {
        System.out.println("Initial CarRentService");
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public boolean save(Object obj) {
        try {
            Connection connection = dataSource.getConnection();
            System.out.println("Save car");
            CarRent o = (CarRent) obj;
            int count = 0;
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO car_rent(TABLE_ID,CAR_RENRATE_ID,PER_HOUR,MINIMUM_CHARGE,SEDAN,VAN,TRIP,SERVICE_TYPE, ");
            sql.append("CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE ) ");
            sql.append("VALUES (?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?,CURRENT_TIMESTAMP)");
            PreparedStatement psmt = connection.prepareStatement(sql.toString());
            psmt.setString(1, o.getTableId());
            psmt.setString(2, o.getCarRentRateId());
            psmt.setInt(3, o.getPerHour());
            psmt.setInt(4, o.getMinimumCharge());
            psmt.setInt(5, o.getSedan());
            psmt.setInt(6, o.getVan());
            psmt.setInt(7, o.getTrip());
            psmt.setInt(8, o.getServiceType());
            psmt.setString(9, o.getCreateBy());
            psmt.setString(10, o.getUpdateBy());

            count = psmt.executeUpdate();
            psmt.close();
            if (psmt != null) {
                psmt = null;
            }

            connection.close();
            if (connection != null) {
                connection = null;
            }
            return (count > 0) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean update(Object obj) {
        try {
            Connection connection = dataSource.getConnection();
            System.out.println("Update carRent");
            CarRent o = (CarRent) obj;
            int count = 0;
            StringBuilder sql = new StringBuilder();
            sql.append("UPDATE car_rent ");
            sql.append("SET CAR_RENRATE_ID=?,PER_HOUR=?,MINIMUM_CHARGE=?,SEDAN=?,VAN=?,TRIP=?,SERVICE_TYPE=?, ");
            sql.append("UPDATE_BY=?,UPDATE_DATE=CURRENT_TIMESTAMP ");
            sql.append("WHERE TABLE_ID=? ");
            PreparedStatement psmt = connection.prepareStatement(sql.toString());
            psmt.setString(1, o.getCarRentRateId());
            psmt.setInt(2, o.getPerHour());
            psmt.setInt(3, o.getMinimumCharge());
            psmt.setInt(4, o.getSedan());
            psmt.setInt(5, o.getVan());
            psmt.setInt(6, o.getTrip());
            psmt.setInt(7, o.getServiceType());
            psmt.setString(8, o.getUpdateBy());
            psmt.setString(9, o.getTableId());
            count = psmt.executeUpdate();
            psmt.close();
            if (psmt != null) {
                psmt = null;
            }

            connection.close();
            if (connection != null) {
                connection = null;
            }
            return (count > 0) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean remove(Object obj) {
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("remove car");
            StringBuilder s = new StringBuilder();
            s.append("DELETE FROM car_rent WHERE TABLE_ID = ?");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement pstm = conn.prepareStatement(s.toString());
            pstm.setString(1, (String) obj);
            int ret = pstm.executeUpdate();
            pstm.close();
            if (pstm != null) {
                pstm = null;
            }

            conn.close();
            if (conn != null) {
                conn = null;
            }
            return (ret == 1) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public Object findById(String id) {
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("FindById");
            StringBuilder s = new StringBuilder();
            s.append("SELECT ");
            s.append("IFNULL(A.TABLE_ID, '-') AS TABLE_ID,");
            s.append("IFNULL(A.CAR_RENRATE_ID, '-') AS CAR_RENRATE_ID,");
            s.append("IFNULL(A.PER_HOUR, 0) AS PER_HOUR,");
            s.append("IFNULL(A.MINIMUM_CHARGE, 0) AS MINIMUM_CHARGE,");
            s.append("IFNULL(A.SEDAN, 0) AS SEDAN,");
            s.append("IFNULL(A.VAN, 0) AS VAN,");
            s.append("IFNULL(A.TRIP, 0) AS TRIP,");
            s.append("IFNULL(A.SERVICE_TYPE, '0') AS SERVICE_TYPE,");
            s.append("IFNULL(A.CREATE_BY, '-') AS CREATE_BY,");
            s.append("IFNULL(A.CREATE_DATE, CURRENT_TIMESTAMP) AS CREATE_DATE,");
            s.append("IFNULL(A.UPDATE_BY, '-') AS UPDATE_BY,");
            s.append("IFNULL(A.UPDATE_DATE, CURRENT_TIMESTAMP) AS UPDATE_DATE ");
            s.append("FROM car_rent A WHERE A.TABLE_ID = ? ");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement pstm = conn.prepareStatement(s.toString());
            pstm.setString(1, id);
            ResultSet rs = pstm.executeQuery();
            CarRent o = new CarRent();
            if (rs.next()) {
                o.setTableId(rs.getString("TABLE_ID"));
                o.setCarRentRateId(rs.getString("CAR_RENRATE_ID"));
                o.setPerHour(rs.getInt("PER_HOUR"));
                o.setMinimumCharge(rs.getInt("MINIMUM_CHARGE"));
                o.setSedan(rs.getInt("SEDAN"));
                o.setVan(rs.getInt("VAN"));
                o.setTrip(rs.getInt("TRIP"));
                o.setServiceType(rs.getInt("SERVICE_TYPE"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(new Date(rs.getDate("CREATE_DATE").getTime()));
                //o.setCreateDate(new Date());
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(new Date(rs.getDate("UPDATE_DATE").getTime()));
                //o.setUpdateDate(new Date());
            }
            rs.close();
            if (rs != null) {
                rs = null;
            }

            pstm.close();
            if (pstm != null) {
                pstm = null;
            }

            conn.close();
            if (conn != null) {
                conn = null;
            }
            return o;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public List find(Object[] obj) {
        List<Driver> list = new ArrayList<Driver>();
        try {
            Connection connection = dataSource.getConnection();
            System.out.println("Save Driver");
            connection.close();
            return list;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public List<CarRent> search(int offset, Object[] certiria) {
        System.out.println("CarRentService search...");
        List<CarRent> list = new ArrayList<CarRent>();
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            StringBuilder s = new StringBuilder();
            s.append("SELECT ");
            s.append("IFNULL(A.TABLE_ID, '-') AS TABLE_ID,");
            s.append("IFNULL(A.CAR_RENRATE_ID, '-') AS CAR_RENRATE_ID,");
            s.append("IFNULL(A.PER_HOUR, 0) AS PER_HOUR,");
            s.append("IFNULL(A.MINIMUM_CHARGE, 0) AS MINIMUM_CHARGE,");
            s.append("IFNULL(A.SEDAN, 0) AS SEDAN,");
            s.append("IFNULL(A.VAN, 0) AS VAN,");
            s.append("IFNULL(A.TRIP, 0) AS TRIP,");
            s.append("IFNULL(A.SERVICE_TYPE, '0') AS SERVICE_TYPE,");
            s.append("IFNULL(A.CREATE_BY, '-') AS CREATE_BY,");
            s.append("IFNULL(A.CREATE_DATE, CURRENT_TIMESTAMP) AS CREATE_DATE,");
            s.append("IFNULL(A.UPDATE_BY, '-') AS UPDATE_BY,");
            s.append("IFNULL(A.UPDATE_DATE, CURRENT_TIMESTAMP) AS UPDATE_DATE, ");    
            s.append("(SELECT IFNULL(B.LOOKUP_NAME,'-') FROM lookup B WHERE B.LOOKUP_VALUE = A.SERVICE_TYPE AND B.LOOKUP_GROUP_ID = 'SERVICE_TYPE' ) AS SERVICE_TYPE_NAME,");
            s.append("(SELECT IFNULL(B.USER_NAME, '-') FROM users B WHERE B.id = A.CREATE_BY ) AS CREATE_BY_NAME, ");
            s.append("(SELECT IFNULL(B.USER_NAME, '-') FROM users B WHERE B.id = A.UPDATE_BY ) AS UPDATE_BY_NAME ");
            s.append("FROM car_rent A WHERE A.TABLE_ID LIKE ? ");
            s.append("LIMIT ? OFFSET ?");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement stmt = conn.prepareStatement(s.toString());
            String name = (String) certiria[0];
            name = (name != null) ? name.replace('*', '%') : "";
            if (name.indexOf("%") == -1) {
                name = "%" + name + "%";
            }
            stmt.setString(1, name);
            stmt.setInt(2, limit);
            stmt.setInt(3, offset);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                CarRent o = new CarRent();
                o.setTableId(rs.getString("TABLE_ID"));
                o.setCarRentRateId(rs.getString("CAR_RENRATE_ID"));
                o.setPerHour(rs.getInt("PER_HOUR"));
                o.setMinimumCharge(rs.getInt("MINIMUM_CHARGE"));
                o.setSedan(rs.getInt("SEDAN"));
                o.setVan(rs.getInt("VAN"));
                o.setTrip(rs.getInt("TRIP"));
                o.setServiceType(rs.getInt("SERVICE_TYPE"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(new Date(rs.getDate("CREATE_DATE").getTime()));
                //o.setCreateDate(new Date());
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(new Date(rs.getDate("UPDATE_DATE").getTime()));
                //o.setUpdateDate(new Date());
                o.setServiceTypeName(rs.getString("SERVICE_TYPE_NAME"));
                o.setCreateByName(rs.getString("CREATE_BY_NAME"));
                o.setUpdateByName(rs.getString("UPDATE_BY_NAME"));
                list.add(o);
            }
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (Exception ex) {
                }
            }
        }
        return list;
    }

    public boolean removeAll(String[] ids) {
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("Save Driver");
            StringBuilder s = new StringBuilder();
            s.append("DELETE FROM car_rent WHERE TABLE_ID = ?");
            System.out.println("sql[" + s.toString() + "]");
            Statement pstm = conn.createStatement();
            for (int i = 0; i < ids.length; i++) {
                pstm.addBatch("DELETE FROM CAR_RENT WHERE TABLE_ID = '" + ids[i] + "'");
            }
            int ret[] = pstm.executeBatch();
            pstm.close();
            if (pstm != null) {
                pstm = null;
            }
            conn.close();
            if (conn != null) {
                conn = null;
            }
            return (ret.length == ids.length) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public int searchCount(int offset, Object[] certiria) {
        int count = 0;
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            StringBuilder s = new StringBuilder();
            s.append("SELECT COUNT(*) FROM car_rent WHERE TABLE_ID LIKE ?");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement stmt = conn.prepareStatement(s.toString());
            String name = (String) certiria[0];
            name = (name != null) ? name.replace('*', '%') : "";
            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (Exception ex) {
                }
            }
        }
        return count;
    }

    public Object findById(String uid, String id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
   
}
